package com.ruoyi.web.controller.gwc;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TRepairManagement;
import com.ruoyi.gwc.service.ITRepairManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维修管理Controller
 * 
 * @author wp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/gwc/repairManagement")
public class TRepairManagementController extends BaseController
{
    @Autowired
    private ITRepairManagementService tRepairManagementService;

    /**
     * 查询维修管理列表
     */
    @ApiOperation("查询维修管理列表")
    @GetMapping("/list")
    public TableDataInfo list(TRepairManagement tRepairManagement)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tRepairManagement.getCompanyId()))) {
            tRepairManagement.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TRepairManagement> list = tRepairManagementService.selectTRepairManagementList(tRepairManagement);
        for (TRepairManagement repairManagement : list ){
            if(ObjectUtils.isNotEmpty(repairManagement.getRepairCost())) {
                repairManagement.setRepairCostStr(repairManagement.getRepairCost().toString());
            }
            if(ObjectUtils.isNotEmpty(repairManagement.getExpectCost())){
                repairManagement.setExpectCostStr(repairManagement.getExpectCost().toString());
            }
            if(ObjectUtils.isNotEmpty(repairManagement.getRepairRepair())){
                repairManagement.setRepairRepairStr(repairManagement.getRepairRepair().toString());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出维修管理列表
     */
    @Log(title = "维修管理", businessType = BusinessType.EXPORT)
    @ApiOperation("导出维修管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRepairManagement tRepairManagement)
    {
        List<TRepairManagement> list = tRepairManagementService.selectTRepairManagementList(tRepairManagement);
        ExcelUtil<TRepairManagement> util = new ExcelUtil<TRepairManagement>(TRepairManagement.class);
        util.hideColumn("updateTime");
        util.hideColumn("updateBy");
        util.hideColumn("remark");
        util.exportExcel(response, list, "维修管理数据");
    }

    /**
     * 获取维修管理详细信息
     */
    @ApiOperation("获取维修管理详细信息")
    @GetMapping(value = "/{repairId}")
    public AjaxResult getInfo(@PathVariable("repairId") Long repairId)
    {
        return success(tRepairManagementService.selectTRepairManagementByRepairId(repairId));
    }

    /**
     * 新增维修管理
     */
    @ApiOperation("新增维修管理")
    @Log(title = "维修管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRepairManagement tRepairManagement)
    {
        tRepairManagement.setRepairId(SnowFlakeIDGenerator.getInstance().nextId());
        tRepairManagement.setRepairCost(new BigDecimal(tRepairManagement.getRepairCostStr()));
        tRepairManagement.setRepairRepair(new BigDecimal(tRepairManagement.getRepairRepairStr()));
        tRepairManagement.setExpectCost(new BigDecimal(tRepairManagement.getExpectCostStr()));

        return toAjax(tRepairManagementService.save(tRepairManagement));
    }

    /**
     * 修改维修管理
     */
    @ApiOperation("修改维修管理")
    @Log(title = "维修管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRepairManagement tRepairManagement)
    {
        tRepairManagement.setRepairCost(new BigDecimal(tRepairManagement.getRepairCostStr()));
        tRepairManagement.setRepairRepair(new BigDecimal(tRepairManagement.getRepairRepairStr()));
        tRepairManagement.setExpectCost(new BigDecimal(tRepairManagement.getExpectCostStr()));
        return toAjax(tRepairManagementService.saveOrUpdate(tRepairManagement));
    }

    /**
     * 删除维修管理
     */
    @ApiOperation("删除维修管理")
    @Log(title = "维修管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairIds}")
    public AjaxResult remove(@PathVariable Long[] repairIds)
    {
        return toAjax(tRepairManagementService.deleteTRepairManagementByRepairIds(repairIds));
    }

    @Log(title ="维修凭证上传图片")
    @ApiOperation("维修凭证上传图片")
    @PostMapping("/uploadRepair")
    public AjaxResult uploadRepair(@RequestBody MultipartFile[] file) {
        return success(uploadFile(file, "repair"));
    }

}
